package org.bees.cms.web.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.bees.cms.web.util.velocity.Context;
import org.bees.cms.web.util.velocity.element.Element;

public abstract class Tag extends TagSupport implements Element {
	private static final long serialVersionUID = 2186672596319392328L;
	protected String id = "";
	protected String clazz = "";
	protected String style = "";

	@Override
	public int doStartTag() throws JspException {
		try {
			before();
			JspWriter writer = pageContext.getOut();
			writer.println();
			analyze(writer);
			writer.println();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				pageContext.getOut().append("标签解析出错,请确认参数准确性!");
			} catch (IOException ioException) {
				ioException.printStackTrace();
			}
		}
		return super.doStartTag();
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			after();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	protected Context context() {
		Context context = new Context();
		context.put("id", id);
		context.put("clazz", clazz);
		context.put("style", style);
		return context;
	}

	@Override
	public String toHTML() throws Exception {
		return null;
	}

	@Override
	public String fromHTML(String html) throws Exception {
		return null;
	}

	protected abstract void before() throws Exception;

	protected abstract void analyze(JspWriter writer) throws Exception;

	protected abstract void after() throws Exception;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}
}
